/**
 * @class UI-条目组件-介绍面板行
 * @author DuskyHuang 圣子
 * @description 实现IItem接口，负责渲染IIntroLineData数据。
*/

import { _decorator, Component, Label, Sprite, UITransform } from 'cc';
import { utils, IItem } from 'cfw';
import { IIntroLineData } from '../../celestial';
import { LabelI18n } from '../label-i18n';
const { ccclass } = _decorator;
const { findComp } = utils;

@ccclass('cocosmos.intro-line')
export class IntroLine extends Component implements IItem<IIntroLineData>{

    private _pad: Sprite = null;
    private _tag: LabelI18n = null;
    private _description: LabelI18n = null;
    private _tagUit: UITransform = null;
    private _descUit: UITransform = null;

    protected onLoad(): void {
        this._pad = this.getComponent(Sprite);
        this._tag = findComp(LabelI18n, this, 'Tag');
        this._description = findComp(LabelI18n, this, 'Description');
        this._tagUit = this._tag.getComponent(UITransform)!;
        this._descUit = this._description.getComponent(UITransform)!;
    }

    public onDataUpdated(data: IIntroLineData): void {
        const { tag, value } = data;
        const desc = value ? (isStr(value) ? value : value.en) : null;
        const hasTag = !!tag, hasDesc = !!desc;
        this._tag.node.active = hasTag;
        this._tagUit.width = hasDesc ? TAG_WIDTH : LINE_WIDTH;
        this._tag.node.setPosition(hasDesc ? TAG_OFFSET : 0, 0);
        this._description.node.active = hasDesc;
        this._descUit.width = hasTag ? DESC_WIDTH : LINE_WIDTH;
        this._description.node.setPosition(hasTag ? DESC_OFFSET : 0, 0);
        if (hasTag) this._tag.string = (tag.length <= 1 ? 'phys@' : 'tags@') + tag;
        if (hasDesc) {
            if (!isStr(value)) {
                this._description.config = value;
            } else {
                const un_forced = FORCE_I18N_DESC.excludes(desc);
                const un_i18n = un_forced && (hasTag && PURE_NUM_TAGS.includes(tag));
                if (un_i18n) {
                    this._description.enabled = false;
                    this._description.getComponent(Label).string = desc;
                } else {
                    this._description.enabled = true;
                    this._description.string = (!un_forced ? 'misc@' : '') + desc;
                }
            }
        }
        this._pad.enabled = this.node.parent.children.indexOf(this.node) % 2 === 0;
        this.adjustHeight();
    }

    public adjustHeight(): void {
        this.scheduleOnce(() => this.node.getComponent(UITransform)!.height
            = Math.max(this._tag.node.active ? this._tagUit!.height : 0,
                this._description.node.active ? this._descUit!.height : 0));
    }
}

// 纯数值不进行翻译
const PURE_NUM_TAGS = [
    'seq', 'diam', 'date', 'ksn',
    'aid', 'cid', 'radius', 'lon', 'obl', 'ecc', 'prd', 'agl',
    'nra', 'nd', '_obl', '_prd',
    'max', 'total', 'weight', 'distances',
    'fid', 'hip', 'hd', 'ra', 'dec', 'brc', 'brs',
    'mag', 'abs', 'spec', 'mul', 'swz', 'csr',
    's', 'v', 'm', 'd', 'g', 'e', 'r', 't', 'a',
    'p', 'h', 'x', 'z', 'n', 'c', 'b',
    'o', 'l', 'i', 'k',
];

// 虽然是PureNumTag
const FORCE_I18N_DESC = [
    'bh', 'sync', 'true', 'false'
];

const TAG_WIDTH = 120;
const TAG_OFFSET = -115;
const DESC_WIDTH = 230;
const DESC_OFFSET = 60;
const LINE_WIDTH = 340;